﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpShare.Storage.Security;

namespace SharpShare.Storage {
    public abstract class StorageItem : IStorageItem {
        protected StorageItem(string name, IStorageContainer parent) {
            this.Name = name;
            this.Parent = parent;
            this.DateCreated = DateTime.Now;
            this.DateModified = DateTime.Now;
        }

        public string Name { get; private set; }
        public StorageItemAttributes Attributes { get; set; }
        public DateTime DateCreated { get; set; }
        public DateTime DateModified { get; set; }
        public StorageAccessList AvailableAccess { get; set; }

        protected virtual void ChangeAttributes(StorageItemAttributes newAttributes) {
            throw new StorageItemUnauthorizedException();
        }

        protected virtual void ChangeDateCreated(DateTime newDate) {
            throw new StorageItemUnauthorizedException();
        }

        protected virtual void ChangeDateModified(DateTime newDate) {
            throw new StorageItemUnauthorizedException();
        }

        protected virtual void Delete() {
            throw new StorageItemUnauthorizedException();
        }

        #region IStorageItem Members

        string IStorageItem.Name {
            get { return this.Name; }
        }

        public IStorageContainer Parent { get; private set; }

        bool IStorageItem.Exists {
            get { return true; }
        }

        StorageItemAttributes IStorageItem.Attributes {
            get {
                return this.Attributes;
            }
            set {
                ChangeAttributes(value);
            }
        }

        DateTime IStorageItem.DateCreated {
            get {
                return this.DateCreated;
            }
            set {
                ChangeDateCreated(value);
            }
        }

        DateTime IStorageItem.DateModified {
            get {
                return this.DateModified;
            }
            set {
                ChangeDateModified(value);
            }
        }

        IStorageDataProvider IStorageItem.OpenMetadata(string name, StorageFileAccess access, StorageFileAccess share, StorageFileMode mode) {
            if (mode == StorageFileMode.OpenExisting) {
                return null;
            }

            throw new StorageItemUnauthorizedException();
        }

        IEnumerable<string> IStorageItem.ListMetadata() {
            yield break;
        }

        bool IStorageItem.RemoveMetadata(string name) {
            return false;
        }

        void IStorageItem.Delete() {
            this.Delete();
        }

        void IStorageItem.Move(IStorageContainer container, string newNameOrNull) {
            throw new StorageItemUnauthorizedException();
        }

        Security.StorageAccessList IStorageItem.AvailableAccess {
            get { return (this.AvailableAccess ?? StorageAccessList.Empty); }
        }

        #endregion

        #region ICastable<IStorageContainer> Members

        IStorageContainer ICastable<IStorageContainer>.TryCast() {
            return (this as IStorageContainer);
        }

        #endregion

        #region ICastable<IStorageFile> Members

        IStorageFile ICastable<IStorageFile>.TryCast() {
            return (this as IStorageFile);
        }

        #endregion
    }
}
